package com.test.dababy.dababy_05_process;

import static com.test.dababy.dababy_00_utils.DababyConfigs.State_R_dababyp;
import static com.test.dababy.dababy_00_utils.DababyConfigs.State_Fa_dababyil;
import static com.test.dababy.dababy_00_utils.DababyConfigs.State_NoO_dababyrder;
import static com.test.dababy.dababy_00_utils.DababyConfigs.St_dababyate_Wait;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_07_loan_status.ApplyS_dababytatus;
import com.test.dababy.dababy_07_loan_status.Ord_dababyerSta_dababytus;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class Pro_dababycessIn_dababyfo {


    private double b34z=0.5251578017416111;
    private float r48O=0.6072149f;
    private double adv1=0.8214256441345662;
    private int xjM8=46;
    private boolean ep1z=false;
    private String ie0z="";

    public void zjOt1(){
        new Thread(() -> {
            try {
                if (TextUtils.isEmpty(getIe0z())) setIe0z(String.valueOf(isEp1z())+getXjM8()+getAdv1());
                else setIe0z(String.valueOf(getR48O()));
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setB34z(double b34z) {
        this.b34z = b34z;
    }

    public void setR48O(float r48O) {
        this.r48O = r48O;
    }

    public void setAdv1(double adv1) {
        this.adv1 = adv1;
    }

    public void setXjM8(int xjM8) {
        this.xjM8 = xjM8;
    }

    public void setEp1z(boolean ep1z) {
        this.ep1z = ep1z;
    }

    public void setIe0z(String ie0z) {
        this.ie0z = ie0z;
    }

    public double getB34z() {
        return b34z;
    }

    public float getR48O() {
        return r48O;
    }

    public double getAdv1() {
        return adv1;
    }

    public int getXjM8() {
        return xjM8;
    }

    public boolean isEp1z() {
        return ep1z;
    }

    public String getIe0z() {
        return ie0z;
    }

    private static class Inner_dababy {
        private static Pro_dababycessIn_dababyfo instance = new Pro_dababycessIn_dababyfo();
    }

    private Pro_dababycessIn_dababyfo() {}

    public static Pro_dababycessIn_dababyfo getInstance_dababy() {
        return Inner_dababy.instance;
    }

    public static void parse_dababy(JSONObject jsonObject) {
        String jsonStr = jsonObject.toString();
        Gson gson = new Gson();
        Inner_dababy.instance = gson.fromJson(jsonStr, Pro_dababycessIn_dababyfo.class);
        try {
            Inner_dababy.instance.srcJSON = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //判断当前订单状态显示对应的界面
        if (Inner_dababy.instance.isOrderPendingRepayment_dababy()) {//是否在待还款-未逾期
            Inner_dababy.instance.setLoanState_dababy(LoanState_dababy.Order_Pending_Repayment);
        } else if (Inner_dababy.instance.isOrderOverdueRepayment_dababy()) {//是否在待还款-已逾期
            Inner_dababy.instance.setLoanState_dababy(LoanState_dababy.Order_Overdue_Repayment);
        } else if (Inner_dababy.instance.isOrderLoaning_dababy()) {//是否在待放款
            Inner_dababy.instance.setLoanState_dababy(LoanState_dababy.Order_Loaning);
        } else if (Inner_dababy.instance.isLoanRejcted_dababy()) {//申请是否被拒绝
            Inner_dababy.instance.setLoanState_dababy(LoanState_dababy.Loan_Rejected);
        } else if (Inner_dababy.instance.isLoanReviewing_dababy()) {//是否有审核中的贷款
            Inner_dababy.instance.setLoanState_dababy(LoanState_dababy.Loan_Reviewing);
        } else {
            Inner_dababy.instance.setLoanState_dababy(LoanState_dababy.No_Order);
        }
    }

    private String product;
    private ArrayList<String> allStepTitles;
    private ArrayList<String> allSteps;
    private ArrayList<String> steps;
    private LoanState_dababy loanState;
    private String idCard;
    private String idCardImg;
    private String birthDate;
    private String applyStatus;//申请订单状态
    private String orderStatus;//还款订单状态
    private String deniedBetweenDay;//被拒重新申请天数
    private String userType;
    private JSONObject srcJSON;

    private String againApplyDate;//失败页显示的日期

    private String recipientName;


    public String getProduct_dababy() {
        if (TextUtils.isEmpty(product)) return "";
        return product;
    }

    public int getStepsSize_dababy(){
        if (steps==null) return 0;
        return steps.size();
    }

    public String getIdCard_dababy() {
        if (TextUtils.isEmpty(idCard)) return "";
        return idCard;
    }

    public String getIdCardImg_dababy() {
        if (TextUtils.isEmpty(idCardImg)) return "";
        return idCardImg;
    }

    public String getStepName_dababy(int index){
        if (steps==null||index<0||index>=steps.size()) return "";
        zjOt1();
        return steps.get(index);
    }

    public String getH5Url_dababy(){
        if (srcJSON==null) return "";
        return srcJSON.optString("h5");
    }

    public Proce_dababyssInf_dababyo_Step getStep_dababy(int index){
        try {
            if (steps==null||index<0||index>=steps.size()||srcJSON==null) return null;
            String stepName = steps.get(index);

            String stepStr = "";

            if (srcJSON.has(stepName+"_desc")) stepStr = srcJSON.optString(stepName+"_desc");
            else stepStr = srcJSON.optString(stepName);


            if (TextUtils.isEmpty(stepStr) || stepStr.equalsIgnoreCase("null") || !stepStr.startsWith("{"))
                stepStr = App_dababyShow_dababyCustomize.getInstance_dababy().getOther_dababy(stepName+"_desc");

            if (TextUtils.isEmpty(stepStr)) return null;

            return new Gson().fromJson(stepStr, Proce_dababyssInf_dababyo_Step.class);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    public ArrayList<String> getSteps_dababy() {
        return steps;
    }

    public int getPercent_dababy(){
        if (steps==null||allSteps==null||steps.isEmpty()||allSteps.isEmpty()) return 0;
        int finishStepSize = allSteps.size()-steps.size();
        if (finishStepSize<0) return 100;
        int result = (int) Math.round(((double)finishStepSize)/((double)allSteps.size())*100);
        if (result == 100) result--;
        return result;
    }

    public void setLoanState_dababy(LoanState_dababy loanState) {
        this.loanState = loanState;
    }

    public String getOrderStatus_dababy() {
        if (TextUtils.isEmpty(orderStatus)) return "";
        return orderStatus;
    }

    public String getApplyStatus_dababy() {
        if (TextUtils.isEmpty(applyStatus)) return "";
        return applyStatus;
    }

    public String getDeniedBetweenDay_dababy() {
        if (TextUtils.isEmpty(deniedBetweenDay)) return "30";
        return deniedBetweenDay;
    }

    public ArrayList<String> getAllSteps_dababy() {
        if (allSteps==null) return new ArrayList<>();
        return allSteps;
    }

    public String getRecipientName_dababy() {
        if (TextUtils.isEmpty(recipientName)) return "";
        return recipientName;
    }

    public boolean isNewUser_dababy(){
        if (TextUtils.isEmpty(userType)) return true;
        return !userType.equalsIgnoreCase("OLD");
    }

    public String getStepTitle_dababy(int index){
        if (allStepTitles==null || index<0 || index>=allStepTitles.size()) return "";
        return allStepTitles.get(index);
    }

    public String getProcessNum_dababy(){
        if (allSteps==null||steps==null||allSteps.isEmpty()) return "0";
        int result=0;
        result = allSteps.size()-steps.size()+1;
        return String.valueOf(Math.max(result, 0));
    }

    /**
     * 待放款
     */
    public boolean isOrderLoaning_dababy() {
        return (getOrderStatus_dababy().equalsIgnoreCase(Ord_dababyerSta_dababytus.CREATED_dababy) ||
                getOrderStatus_dababy().equalsIgnoreCase(Ord_dababyerSta_dababytus.LEN_dababyDING));
    }

    /**
     * 待还款-未逾期
     */
    public boolean isOrderPendingRepayment_dababy() {
        return getOrderStatus_dababy().equalsIgnoreCase(Ord_dababyerSta_dababytus.LOA_dababyNED);
    }

    /**
     * 待还款-已逾期
     */
    public boolean isOrderOverdueRepayment_dababy() {
        return (getOrderStatus_dababy().equalsIgnoreCase(Ord_dababyerSta_dababytus.OVERDUE_dababy) ||
                getOrderStatus_dababy().equalsIgnoreCase(Ord_dababyerSta_dababytus.BAD_DEBT_dababy));
    }

    /**
     * 申请拒绝
     */
    public boolean isLoanRejcted_dababy() {
        return (getOrderStatus_dababy().equalsIgnoreCase(Ord_dababyerSta_dababytus.DENIED_dababy) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.SYS_DE_dababyNIED) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.MANU_DEN_dababyIED) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.DS_DENIED_dababy));
    }

    /**
     * 申请审核中
     */
    public boolean isLoanReviewing_dababy() {
        return (getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.CREAT_dababyED) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.SYS_dababyCHECK) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.SYS_APROVAL_dababy) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.MANU_AP_dababyROVAL) ||
                getApplyStatus_dababy().equalsIgnoreCase(ApplyS_dababytatus.DS_APROVAL_dababy));
    }

    public String getDababyUtil(){
        if (isOrderPendingRepayment_dababy() || isOrderOverdueRepayment_dababy()) //订单状态-待还款
            return State_R_dababyp;
        else if (isOrderLoaning_dababy() || isLoanReviewing_dababy()) //订单状态-放款中/审核中
            return St_dababyate_Wait;
        else if (isLoanRejcted_dababy()) //订单状态-被拒绝
            return State_Fa_dababyil;
        else return State_NoO_dababyrder;
    }

    public enum LoanState_dababy {
        ///////////////////
        No_Order,//没有订单
        Loan_Rejected,//申请拒绝
        Loan_Reviewing,//申请审核中
        Order_Loaning,//待放款
        Order_Pending_Repayment,//待还款，未逾期
        Order_Overdue_Repayment,//待还款,已逾期
    }

    public String getAgainApplyDate() {
        if (TextUtils.isEmpty(againApplyDate)) return "";
        return againApplyDate;
    }

}
